// index.js
import { createRouter, createWebHistory } from 'vue-router'

// 引入页面组件
import Index from '@/views/Index.vue'
import Login from '@/views/Login.vue'
import Admin from '@/views/Admin.vue'


// 创建路由器
const router = createRouter({
    history: createWebHistory(),
    routes: [
        { path: '/login', component: Login },
       /* { path: '/user-login', component: UserLogin },
        { path: '/userregister', component: UserRegister },*/

        // 管理员端布局
        {
            path: '/',
            component: Index,
            children: [
                { path: 'admin', component: Admin },
                { path: 'product', component: Product },

            ]
        }/*,

        // 普通用户端布局
        {
            path: '/userindex',
            component: UserIndex,
            children: [
/!*
                { path: '', component: Introduction }, // 默认首页
*!/
                { path: 'usercourse', component: UserCourse },
                { path: 'introduction', component: UserIntroduction },
                { path: 'userreserve', component: UserReserve },
                { path: 'userchart', component: UserChart },
                { path: 'myvip', component: MyVip },
                { path: 'comment', component: Comment }
            ]
        }*/
    ]
})

// 全局前置守卫
import { useTokenStore } from '@/store/token.js'
import Product from "@/views/Product.vue";



let whiteList = ['/login', '/user-login']
router.beforeEach((to, from, next) => {
    const tokenStore = useTokenStore()
    const token = tokenStore.token
    if (!whiteList.includes(to.path) && !token) {
        next('/login')
    } else {
        next()
    }
})

export default router
